package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.BalanceType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.model.YgUserCultivationDayModel;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.po.YgUserBalanceDetail;
import com.ygqh.baby.service.YgAgentInviteRecordService;
import com.ygqh.baby.service.YgUserBalanceService;
import com.ygqh.baby.service.YgUserCultivationDayService;
import com.ygqh.baby.service.YgUserService;
import com.ygqh.baby.utils.ShareCodeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

@Controller
@RequestMapping("/admin/sourceUser")
public class SourceUserController extends BaseController<Object, java.lang.Long>{

	@Autowired
	private YgUserBalanceService ygUserBalanceService;
	
	@Autowired
	private YgUserService ygUserService;
	@Autowired
	private YgAgentInviteRecordService ygAgentInviteRecordService;
	@Autowired
	private YgUserCultivationDayService ygUserCultivationDayService;
	
	@RequestMapping("searchSourceUser")
	@ResponseBody
	public Map<String,Object> searchSourceUser(String q, QueryInfo queryInfo, String userName, String sourceCode,
											   String userType, String inviteCode, HttpServletRequest request)  {
		Long userId = null;
		if (StringUtils.isNotBlank(inviteCode)) {
			try {
				userId = ShareCodeUtil.codeToId(inviteCode);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		String sortDir = request.getParameter("sSortDir_0");
		String sortCol = request.getParameter("iSortCol_0");
		if (StringUtils.isNotBlank(sortCol) && Integer.valueOf(sortCol) > 0
				&& StringUtils.isNotBlank(sortDir)) {
			queryInfo.setSortKey("cultivation_price");
			queryInfo.setOrderByClause(sortDir);
		}
		ResultSet<YgUser> result=ygUserBalanceService.search(queryInfo, userName, sourceCode, q,userType, userId);
		
		return result.toDatatables(null);
	}
	@RequestMapping("searchTradeDetail")
	@ResponseBody
	public Map<String,Object> searchTradeDetail(QueryInfo queryInfo,Long userId,BalanceType balanceType, String platformNo) {
		ResultSet<YgUserBalanceDetail> result=ygUserBalanceService.searchBalanceDetail(queryInfo, balanceType, userId, platformNo);
		
		return result.toDatatables(null);
	}

	@RequestMapping("updateRate")
	@ResponseBody
	public Message updateRate(Long userId,BigDecimal shareRate,BigDecimal foreverRate){
		BigDecimal n=new BigDecimal("0.01");
		shareRate=shareRate.multiply(n);
		foreverRate=foreverRate.multiply(n);
		return ygUserBalanceService.updateRate( userId,shareRate,foreverRate);
	}
	
	
	@RequestMapping("searchSourceUserByFromCode")
	@ResponseBody
	public Map<String,Object> searchSourceUserByFromCode(String fromCode, String groupTag, QueryInfo queryInfo)  {
		ResultSet<YgUser> result;
		if (StringUtils.isNotBlank(groupTag)) {
			result = ygUserService.search(queryInfo, null, null, null, null,
					null, fromCode, groupTag, null);
		} else {
			YgUser user = ygUserService.findBySourceCode(fromCode);
			result = ygAgentInviteRecordService.searchRecommendUser(queryInfo, user.getId());
		}
		return result.toDatatables(null);
	}
	
	

	
	@RequestMapping("searchUserCultivationMonth")
	@ResponseBody
	public Map<String, Object> searchUserCultivationMonth(Long userId) {
		ResultSet<YgUserCultivationDayModel> resultSet = ygUserCultivationDayService.findUserCultivationSumGroupByMonth(userId);
		return resultSet.toDatatables(null);
	}

}
